/*
 *company:tf56
 *autor:mlc
 *date:2014-8-11
 *description:SelectDateForHuoD
 *vision:1.0
 *
 * */
(function(){
    var getNow=function(){
        var nowTime=new Date(),_time={};
        _time.y=nowTime.getFullYear();
        _time.month=nowTime.getMonth()+1;
        var ld=parseInt(new Date(nowTime.y,nowTime.month,0).getDate());
        if(nowTime.getDate()<ld)
            _time.d=nowTime.getDate()+1;
        else
            _time.d=1;
        _time.h=nowTime.getHours();
        _time.minute=nowTime.getMinutes();
        return _time;
    };
    var nowTime=getNow(),D=document;
    var getId=function(id){
        return D.getElementById(id);
    };
    var cE=function(param){
        return D.createElement(param);
    };
    function getMonLastdate(){
        return  new Date(nowTime.y,nowTime.month,0).getDate();
    }
    var showdate=function(){
        var first_d=getNow().d;
        var d_tbody=getId('m_date');
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            d_tbody.appendChild(tr);

        };
        for(var i=1;i<8;i++){
            var tr=cE('tr');
            var td=cE('td');
            if(first_d<10)
                td.innerHTML='<span>'+'0'+first_d+'</span>';
            else
                td.innerHTML='<span>'+first_d+'</span>';
            if(first_d==getMonLastdate()){
                first_d =0;
            }
            first_d++;
            tr.appendChild(td);
            d_tbody.appendChild(tr);
        };
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            d_tbody.appendChild(tr);
        };
    };
    showdate();
    var showhours=function(){
        var first_h=getNow().h;
        var h_tbody=getId('m_hours');
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            h_tbody.appendChild(tr);
        };
        for(var i=0;i<24;i++){
            var tr=cE('tr');
            var td=cE('td');
            if(i>9)
                td.innerHTML='<span>'+i+'</span>';
            else
                td.innerHTML='<span>'+'0'+i+'</span>';
            tr.appendChild(td);
            h_tbody.appendChild(tr);
        };
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            h_tbody.appendChild(tr);
        };
    };
    showhours();
    var showminite=function(){
        var first_m=getNow().minute;
        var m_tbody=getId('m_minite');
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            m_tbody.appendChild(tr);
        };
        for(var i=0;i<60;i=i+10){
            var tr=cE('tr');
            var td=cE('td');
            if(i!=0)
                td.innerHTML='<span>'+i+'</span>';
            else
                td.innerHTML='<span>'+'00'+'</span>';
            tr.appendChild(td);
            m_tbody.appendChild(tr);
        };
        for(var i=1;i<3;i++){
            var tr=cE('tr');
            var td=cE('td');
            td.innerHTML='<span>'+'&nbsp;'+'</span>';
            tr.appendChild(td);
            m_tbody.appendChild(tr);
        };
    };
    showminite();
    var TFHD_d_obj=getId('m_date'),
        TFHD_h_obj=getId('m_hours'),
        TFHD_m_obj=getId('m_minite'),
        nowaddr  = 0,
        Y1 = 0,
        Y2 = 0;
    function touchChange(obj){
        var max_scroll;
        obj.addEventListener('touchstart',function(ev){
            ev.preventDefault();
            Y1=ev.touches[0].pageY;
            nowaddr=(new WebKitCSSMatrix(window.getComputedStyle(obj).webkitTransform)).f;
            max_scroll=240-obj.offsetHeight;           // alert('max_scroll:'+max_scroll+'..');
        },false);
        obj.addEventListener('touchmove',function(ev){
            ev.preventDefault();
            Y2=ev.touches[0].pageY;
            // var sT= this.style.top.toString().match(/[-]?[0-9]+/);
            if(Y1<Y2){//down
                this.style.webkitTransform='translate3d(0,'+(Y2-Y1+nowaddr)+'px,0)';
            }
            if(Y1>Y2){//up
                this.style.webkitTransform='translate3d(0,'+(Y2-Y1+nowaddr)+'px,0)';
            } 
        },false);
        obj.addEventListener('touchend',function(ev){
            var h=(new WebKitCSSMatrix(window.getComputedStyle(obj).webkitTransform)).f;
            if(h>0){
                this.style.webkitTransform='translate3d(0,'+0+'px,0)';
            }
          
            else if(h<max_scroll){
                this.style.webkitTransform='translate3d(0,'+max_scroll+'px,0)';
            }
            else if(h%48!=0){
                if(Math.abs(h%48)>=24){
                    this.style.webkitTransform='translate3d(0,'+(h-48+Math.abs(h%48))+'px,0)';
                }else{
                    this.style.webkitTransform='translate3d(0,'+(h+Math.abs(h%48))+'px,0)';
                }
            }
            var f_c=(new WebKitCSSMatrix(window.getComputedStyle(obj).webkitTransform)).f/48;
            f_c=Math.abs(f_c);
            switch (obj.id){
                case 'm_date':
                    if((nowTime.d+f_c)<=getMonLastdate())
                        {if(nowTime.month<10)
                            getId('month').innerHTML='0'+nowTime.month;
                        else{
                            getId('month').innerHTML=nowTime.month;
                            if(nowTime.month==12){
                                getId('year').innerHTML=nowTime.y;
                            }
                        }
                        if((nowTime.d+f_c)<10)
                            getId('date').innerHTML='0'+(nowTime.d+f_c);
                        else
                            getId('date').innerHTML=nowTime.d+f_c;}
                    else
                        {if(nowTime.month<9)
                            getId('month').innerHTML='0'+(nowTime.month+1);
                        else {
                            if((nowTime.month+1)>12){
                                getId('month').innerHTML='0'+(nowTime.month+1-12);
                                getId('year').innerHTML=nowTime.y+1;
                            }
                            else
                                getId('month').innerHTML=nowTime.month+1;
                        }
                        if(((nowTime.d+f_c)-getMonLastdate())<10)
                            getId('date').innerHTML='0'+((nowTime.d+f_c)-getMonLastdate());
                        else
                            getId('date').innerHTML=(nowTime.d+f_c)-getMonLastdate();};
                    break;
                case 'm_hours':if(f_c<10) getId('hours').innerHTML='0'+f_c;else getId('hours').innerHTML=f_c;break;
                case 'm_minite':if(f_c==0) getId('minite').innerHTML='00'; else getId('minite').innerHTML=f_c*10;break;
                default:break;
            }
            showColorFont();
        },false)
    }
    touchChange(TFHD_d_obj);
    touchChange(TFHD_h_obj);
    touchChange(TFHD_m_obj);
    var mycalendarinit=function(){
        var m=nowTime.minute,h=nowTime.h,d=nowTime.d;
        if(parseInt(m)/10>-1){
            TFHD_m_obj.style.webkitTransform='translate3d(0,'+(0-parseInt(m/10))*48+'px,0)';
        }
        if(h>1){
            TFHD_h_obj.style.webkitTransform='translate3d(0,'+(0-h)*48+'px,0)';
        }
    };
    mycalendarinit();
    function valing(){
        getId('year').innerHTML=nowTime.y;
        if(nowTime.month<10)
            getId('month').innerHTML='0'+nowTime.month;
        else
            getId('month').innerHTML=nowTime.month;
        if(nowTime.d<10)    
            getId('date').innerHTML='0'+nowTime.d;
        else
            getId('date').innerHTML=nowTime.d;
        if(nowTime.h<10)
            getId('hours').innerHTML='0'+nowTime.h;
        else
            getId('hours').innerHTML=nowTime.h;
        if(parseInt(nowTime.minute/10)==0)
            getId('minite').innerHTML='00';
        else
            getId('minite').innerHTML=parseInt(nowTime.minute/10)*10;
    };
    valing();
    var showColorFont=function(){
        var tempDate=getId("date").innerHTML,
            tempHours=getId("hours").innerHTML,
            tempMinite=getId("minite").innerHTML;
            for(var i=0;i<TFHD_d_obj.children.length;i++){
                if(TFHD_d_obj.children[i].children[0].children[0].innerHTML==tempDate){
                    TFHD_d_obj.children[i].children[0].children[0].className="TFHD_bigShow";
                }else{
                    TFHD_d_obj.children[i].children[0].children[0].className="";
                }
            };
             for(var i=0;i<TFHD_h_obj.children.length;i++){
                if(TFHD_h_obj.children[i].children[0].children[0].innerHTML==tempHours){
                    TFHD_h_obj.children[i].children[0].children[0].className="TFHD_bigShow";
                }else{
                    TFHD_h_obj.children[i].children[0].children[0].className="";
                }
            };
             for(var i=0;i<TFHD_m_obj.children.length;i++){
                if(TFHD_m_obj.children[i].children[0].children[0].innerHTML==tempMinite){
                    TFHD_m_obj.children[i].children[0].children[0].className="TFHD_bigShow";
                }else{
                    TFHD_m_obj.children[i].children[0].children[0].className="";
                }
            };  
    };
    showColorFont();
})();